import gameConfig, {JumpMiniGameConfig} from "../../engines/configs/gameConfig";
import cellMiniGameJump from "../public/cellMiniGameJump";

const {ccclass, property} = cc._decorator;

@ccclass
export default class scrollMiniGameJump extends cc.Component {

    @property(cc.Prefab)
    pbTwoItem: cc.Prefab = null;
    @property(cc.Node)
    ndListContent: cc.Node = null;

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    protected start() {
        this.showAllMiniGameJumpList();
    }

    showAllMiniGameJumpList() {
        const LIST = gameConfig.MINI_GAME_JUMP_SCROLL_LIST;
        cc.Tween.stopAllByTarget(this.ndListContent);
        for (let i = 0; i < LIST.length; i += 2) {
            let oneData = LIST[i];
            let twoData = LIST[i + 1];
            //
            let node = cc.instantiate(this.pbTwoItem);
            this.ndListContent.addChild(node);
            //
            this.controlCellOne(node.getChildByName('cellUp'), oneData);
            this.controlCellOne(node.getChildByName('cellDown'), twoData);
        }
        //下一帧轮播
        this.scheduleOnce(() => {
            if (this.ndListContent.width <= this.node.width) return;
            let length = this.ndListContent.width - this.node.width;
            //
            cc.tween(this.ndListContent)
                .then(
                    cc.tween(this.ndListContent)
                        .to(0, {x: -375})
                        .to(this.ndListContent.width / this.node.width * 5, {x: -375 - length})
                )
                .repeatForever()
                .start();
        })
    }

    protected controlCellOne(node: cc.Node, data: JumpMiniGameConfig) {
        if (data == null) {
            node.active = false;
            return;
        }
        //
        node.getComponent(cellMiniGameJump).setData(data);
    }

    protected onBtnClose() {
        this.ndListContent.destroyAllChildren();
        this.node.destroy();
    }

    // update (dt) {}
}
